<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* 
*/
class StaffReport extends MY_Controller
{
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('Statis/StaffReportModel','staf');
	}

	//
	public function index()
	{
		//获取教练
		$query = $this->db->query("SELECT id, nickname FROM nmp_coach");
		$data['coach_list'] = $query->result_array();

		//获取场馆
		$query = $this->db->query("SELECT id, name FROM nmp_shop");
		$data['venue_list'] = $query->result_array();

		$this->load->view('staff_report',$data);
	}


	//
	public function coach_appoint()
	{
		$date_num = $this->input->get('date_num');

		$res = $this->staf->coach_appoint_data($date_num);

		if ($res) 
		{
			$data = $res;
			$data['status'] = 200;
			$data['message'] = '获取成功！';
		}
		else
		{
			$data['list'] = '';
			$data['status'] = 400;
			$data['message'] = '数据获取失败！';
		}

		echo json_encode($data);exit();
	}


	//员工报表
	public function user_appoint()
	{
		$venue_id = $this->input->get('venue_id');
		$coach_id = $this->input->get('coach_id');
		$dates = $this->input->get('dates');
		$page = $this->input->get('page');
		$page = $page ? $page : 1;

		$res = $this->staf->get_appoint($venue_id,$coach_id,$dates,$page);

		if ($res) 
		{
			$data = $res;
			$data['status'] = 200;
			$data['message'] = '获取成功！';
		}
		else
		{
			$data['list'] = '';
			$data['status'] = 400;
			$data['message'] = '数据获取失败！';
		}

		echo json_encode($data);exit();

	}
	
	//导出员工报表
	public function export_user_appoint(){
		$pdata = $this->input->post();
		
		$farr = array('venue_id','coach_id','dates');
		$arr = array();
		foreach($pdata as $key=>$val){
			if(in_array($key,$farr)){
				$arr[$key] = $val;
			}
		}
		
		$res = $this->staf->get_data($arr);
		
		$head = array('日期','教练','课程名称','工作时间','会员','课程价格(元)');
		$data = array();
		$total = 0;
		
		foreach($res as $key=>$val){
			$data[] = array(
				'addtime'=>$val['addtime'],
				'nickname'=>$val['nickname'],
				'course_name'=>$val['course_name']."({$val['card_name']})",
				'stime'=>$val['stime'],
				'user_name'=>$val['user_name'],
				'price'=>$val['price'],
			);
			$total += $val['price'];
		}
		
		$data[] = array(
			'ctotal'=>'总计',
			'sijiao'=>'私教课：'.count($res)."节",
			'total'=>'总金额：'.$total."元",
		);
		
		$result = $this->putCsv('user_appoint.csv', $data, $head);
		if(!$result){
			$this->err_out('文件导出失败');
		}
		
		$url = base_url($result);
		
		$this->data_out($url);
		
	}
	
	public function putCsv($csvFileName, $dataArr ,$haderText = array()){
		$handle = fopen($csvFileName,"w");//写方式打开
		if(!$handle){
			return false;
		}
		//判断是否定义头标题
		if(!empty($haderText)){
			foreach ($haderText as $key => $value) {
				$haderText[$key] = iconv("utf-8","gbk//IGNORE",$value);//对中文编码进行处理
			}
			
			$re = fputcsv($handle,$haderText);//该函数返回写入字符串的长度。若出错，则返回 false。。
		}
		foreach ($dataArr as $key => $value) {
			foreach ($value as $k => $v) {
				$value[$k] = iconv("utf-8","gbk//IGNORE",$v);//对中文编码进行处理
			}
			$re = fputcsv($handle,$value);//该函数返回写入字符串的长度。若出错，则返回 false。。
		}
		
		fclose($handle);
		return $csvFileName;
	}
	
}



?>